package cn.wolfcode.book.service.impl;

import cn.wolfcode.book.dao.BookDAO;
import cn.wolfcode.book.dao.impl.BookDAOImpl;
import cn.wolfcode.book.entity.Book;
import cn.wolfcode.book.entity.Books;
import cn.wolfcode.book.service.BookService;
import cn.wolfcode.entity.PageResult;
import cn.wolfcode.entity.QueryObject;

import java.util.List;

/**
 * @author 李新雨的项目86176
 * @version 1.0
 * @date 2024-10-28 12:42
 */
public class BookServiceImpl implements BookService {
    /**
     * 编译期耦合度高
     */
    private final BookDAO bookDAO=new BookDAOImpl();
    @Override
    public void delById(Long id) {
        bookDAO.delById(id);
    }

    @Override
    public Book selOneById(Long id) {
        return bookDAO.selOneById(id);
    }

    @Override
    public List<Book> selAll() {
        return bookDAO.selAll();
    }


    @Override
    public void save(Book book) {
     //todo 获取页面的id
        Long id = book.getId();
        if (id!=null){
            book.setId(id);
            bookDAO.edit(book);
        }else {
            bookDAO.add(book);
        }
    }

    @Override
    public PageResult<Book> query(QueryObject qo) {
        //总数据条数
        int totalCount = bookDAO.selCount(qo);
        if(totalCount<=0){
            return new PageResult<>();
        }
        //// TODO: 2024/11/5  假分页  数据在内存中存储
        List<Book> books = bookDAO.selList(qo);

        //总页数
       // int totalPage = totalCount%qo.getPageSize()==0?totalCount/qo.getPageSize():
                //totalCount/qo.getPageSize()+1;
        //
        //int prevPage = qo.getCurrentPage()-1>=1?qo.getCurrentPage()-1:1;
        //int nextPage = qo.getCurrentPage()+1<=totalPage?qo.getCurrentPage()+1:totalPage;
        //
        return new PageResult<Book> (qo.getCurrentPage(),qo.getPageSize(),
                                      totalCount,books);
    }


//真分页
    public void T(QueryObject queryObject){
        List<Book> books = bookDAO.selAllTrue(queryObject);
        Books books1=new Books();
        for (Book book : books) {
            books1.setId(book.getId());
            books1.setBname(book.getBname());
            books1.setAuthor(book.getAuthor());
            bookDAO.addT(books1);
        }
    }

}
